package com.sbw.java.advanced.demo.config;

import javax.sql.DataSource;

import com.sbw.java.advanced.demo.common.Constants;
import com.sbw.java.advanced.demo.utils.MybatisUtils;
import com.sbw.java.advanced.demo.JavaAdvancedDemoApplication;
import com.sbw.java.advanced.demo.utils.DataSourceUtils;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.github.pagehelper.PageHelper;

/**
 * @author sangbowei
 * @date 2018年7月6日
 * 
 */
@Configuration
@EnableConfigurationProperties(DruidMysql.class)
@MapperScan(basePackages = Constants.MAPPER_MYSQL_PATH, sqlSessionFactoryRef = "mysqlSqlSessionFactory")
@EnableTransactionManagement
public class MysqlDataSourceConfig {
	
	private static Logger logger = LoggerFactory.getLogger(JavaAdvancedDemoApplication.class);
	
	@Autowired
	private DruidMysql druidMysql;
	
	@Bean(name = "mysqlDataSource")
	@Primary
	public DataSource mysqlDataSource() {
		DataSource dataSource = DataSourceUtils.getDataSource(druidMysql);
		return dataSource;
	}
	
	@Bean(name = "mysqlSqlSessionFactory")
	@Primary
    public SqlSessionFactory mysqlSqlSessionFactory() {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(mysqlDataSource());
        bean.setTypeAliasesPackage(Constants.DO_MAIN_MYSQL_PATH);

        //添加插件
        PageHelper pageHelper = MybatisUtils.getPageHelper();
        bean.setPlugins(new Interceptor[]{pageHelper});

        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            bean.setMapperLocations(resolver.getResources(Constants.MAPPER_MYSQL_XML_PATH));
            return bean.getObject();
        } catch (Exception e) {
        	logger.error("build mysql SqlSessionFactory has error !");
            throw new RuntimeException(e);
        }
    }
	
	//配置事务管理器
	@Bean(name = "mysqlTransactionManager")
	@Primary
	public DataSourceTransactionManager mysqlTransactionManager() {
		return new DataSourceTransactionManager(mysqlDataSource());
	}
	
	//配置模板
    @Bean(name = "mysqlSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory")SqlSessionFactory sqlSessionFactory) {
    	return new SqlSessionTemplate(sqlSessionFactory);
    }

}
